﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.TownHall.ServiceClient;
using Microsoft.Phone.Tasks;
using NASA.BeAMartian.Model;
using NASA.BeAMartian.Helper;

namespace NASA.BeAMartian.Views
{
    public partial class RegisterChild : PhoneApplicationPage
    {
        public THServiceClient client = new THServiceClient();
        ClientState _clientState = new ClientState();
        User usr = new User();

        public RegisterChild()
        {
            InitializeComponent();
            client.RegistrationCompleted += new RegistrationEventHandler(client_RegistrationCompleted);
            Loaded += new RoutedEventHandler(RegisterAdult_Loaded);
        }

        void RegisterAdult_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.URL = (sender as HyperlinkButton).Tag.ToString();
            wbt.Show();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;

            if (rb.GroupName == "Avatar")
                usr.Avatar = rb.Tag.ToString();
            else
                usr.CitizenType = rb.Content.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string error = "";

            if (pwConfirm.Password != pwNew.Password)
                error += "Passwords do not match.\r\n";

            if (pwNew.Password.Length < 4)
                error += "Password must be at least 4 characters.\r\n";

            if (!tbParentEmail.Text.Contains("@") || !tbParentEmail.Text.Contains("."))
                error += "A parent email is invalid.\r\n";

            if (tbUsername.Text.Length == 0)
                error += "Username is required.\r\n";

            if (cbCivicAgree.IsChecked.Value == false || cbTerms.IsChecked.Value == false ||
                cbTerms_Copy.IsChecked.Value == false || cbTerms_Copy1.IsChecked.Value == false)
                error += "Terms of use must be accepted.\r\n";

            if (error.Length > 0)
            {
                error = "Please correct the following errors:\r\n\r\n" + error;
                MessageBox.Show(error);
                return;
            }

            usr.ParentEmail = tbParentEmail.Text;
            usr.DisplayName = tbUsername.Text;
            usr.IsUnder13 = true;
            (sender as Button).IsEnabled = false;
            client.Register(usr, pwNew.Password, pwConfirm.Password);
        }

        void client_RegistrationCompleted(object sender, RegistrationCompletedEventArgs e)
        {
            Action<RegistrationCompletedEventArgs> act = new Action<RegistrationCompletedEventArgs>(DisplayResponse);
            this.Dispatcher.BeginInvoke(act, e);
        }

        void DisplayResponse(RegistrationCompletedEventArgs e)
        {
            MessageBox.Show("Thank you for registering!");
            _clientState.CurrentUser = usr;
            ApplicationContext.Instance.UserData = usr;
            var store = new PersistentDataStorage();
            store.Backup("User", ApplicationContext.Instance.UserData);

            NavigationService.Navigate(new Uri("/Welcome.xaml", UriKind.Relative));
        }
    }
}